package com;

import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;



public class Form1_emplist implements ActionListener{
	/*Linux JDBC related code start*/
	private static final String dbClassName = "com.mysql.jdbc.Driver";
	private static final String CONNECTION = "jdbc:mysql://127.0.0.1/test";
	/*Linux JDBC related code end*/
	
	JFrame f;
	JLabel l1, l2, l3, l4;
	JTextField t1, t2, t3, t4;
	JButton b1, b2, b3, b4;
	JComboBox cbox1;
	GridLayout g1;
	
	public Form1_emplist (){
		f = new JFrame("Emplist");
		g1 = new GridLayout(6,2);
		f.setLayout(g1);
		//cbox_list
		cbox1 = new JComboBox();
		
		l1 = new JLabel("Select employee id");
		l2 = new JLabel("Name");
		l3 = new JLabel("Address");
		l4 = new JLabel("Contact");
		t1 = new JTextField();
		t2 = new JTextField();
		t3 = new JTextField();
		b1 = new JButton("Add");
		b2 = new JButton("Update");
		b3 = new JButton("Delete");
		b4 = new JButton("Reset");
		
		f.add(l1);
		f.add(cbox1);
		f.add(l2);
		f.add(t1);
		f.add(l3);
		f.add(t2);
		f.add(l4);
		f.add(t3);
		f.add(b1);
		f.add(b2);
		f.add(b3);
		f.add(b4);
		
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
				
		f.setVisible(true);
		f.setSize(500, 500);
		
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
	}

	public void actionPerformed(ActionEvent e){
		try{
			/*Linux JDBC related code start*/
			Properties p = new Properties();
		    p.put("user","root");
		    p.put("password","dkit");/*Linux JDBC related code end*/
		    
			Class.forName(dbClassName);
			Connection con = DriverManager.getConnection(CONNECTION,p);
			String query = "Insert into emp (id, ename, eadd, esal) values ('',?, ?, ?)";
			PreparedStatement pd = con.prepareStatement(query);
			pd.setString(1,t1.getText());
			pd.setString(2,t2.getText());
			pd.setString(3,t2.getText());
			//l4.setText("You have registered with us");
			int i = pd.executeUpdate();
			if(i>0){
				JOptionPane.showMessageDialog(f, "Your details has been inserted");
			}
			con.close();
		}
		catch (Exception ex){
			JOptionPane.showMessageDialog(f, "Try again");
		}
	}

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
